package com.jhte.scheduleJob.controller;

import com.jhte.scheduleJob.base.util.BaseMsg;
import com.jhte.scheduleJob.base.util.BaseResponse;
import com.jhte.scheduleJob.entity.ScheduleJob;
import com.jhte.scheduleJob.service.ScheduleJobService;
import com.jhte.scheduleJob.vo.JobKeyRequest;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wuchun
 * @description
 * @create 2018-01-31 11:09
 */
@RestController
@RequestMapping(value = "/scheduleJob")
public class ScheduleJobController {

    @Autowired
    private ScheduleJobService scheduleJobService;

    private static Logger logger = LoggerFactory.getLogger(ScheduleJobController.class);

    /**
     * 添加任务
     *
     * @param scheduleJob
     * @return
     */
    @PostMapping(value = "/addScheduleJob")
    public BaseResponse addScheduleJob(@RequestBody ScheduleJob scheduleJob) {
        logger.info("Enter ScheduleJob API /scheduleJob/addScheduleJob");
        BaseResponse baseResponse = new BaseResponse();
        try {
            scheduleJobService.addScheduleJob(scheduleJob);
            baseResponse.success();
        } catch (SchedulerException e) {
            baseResponse.setCode(BaseMsg.CODE_EXCEPTION);
            baseResponse.setMsg("创建定时任务失败");
            logger.error("Execute API /scheduleJob/addScheduleJob failed!", e);
        } catch (Exception e) {
            baseResponse.exception();
            logger.error("Execute API /scheduleJob/addScheduleJob failed!", e);
        }
        return baseResponse;
    }

    /**
     * 更新任务
     *
     * @param scheduleJob
     * @return
     */
    @PostMapping(value = "/updateScheduleJob")
    public BaseResponse updateScheduleJob(@RequestBody ScheduleJob scheduleJob) {
        BaseResponse baseResponse = new BaseResponse();
        try {
            scheduleJobService.updateScheduleJob(scheduleJob);
            baseResponse.success();
        } catch (SchedulerException e) {
            baseResponse.setCode(BaseMsg.CODE_EXCEPTION);
            baseResponse.setMsg("更新定时任务失败");
            logger.error("Execute API /scheduleJob/updateScheduleJob failed!", e);
        } catch (Exception e) {
            baseResponse.exception();
            logger.error("Execute API /scheduleJob/updateScheduleJob failed!", e);
        }
        return baseResponse;
    }

    /**
     * 暂停任务
     *
     * @param jobKeyRequest
     * @return
     */
    @PostMapping(value = "/pauseScheduleJob")
    public BaseResponse pauseScheduleJob(@RequestBody JobKeyRequest jobKeyRequest) {
        BaseResponse baseResponse = new BaseResponse();
        try {
            scheduleJobService.pauseScheduleJob(jobKeyRequest);
            baseResponse.success();
        } catch (SchedulerException e) {
            baseResponse.setCode(BaseMsg.CODE_EXCEPTION);
            baseResponse.setMsg("暂停定时任务失败");
            logger.error("Execute API /scheduleJob/pauseScheduleJob failed!", e);
        } catch (Exception e) {
            baseResponse.exception();
            logger.error("Execute API /scheduleJob/pauseScheduleJob failed!", e);
        }
        return baseResponse;
    }

    /**
     * 恢复任务
     *
     * @param jobKeyRequest
     * @return
     */
    @PostMapping(value = "/resumeScheduleJob")
    public BaseResponse resumeScheduleJob(@RequestBody JobKeyRequest jobKeyRequest) {
        BaseResponse baseResponse = new BaseResponse();
        try {
            scheduleJobService.resumeScheduleJob(jobKeyRequest);
            baseResponse.success();
        } catch (SchedulerException e) {
            baseResponse.setCode(BaseMsg.CODE_EXCEPTION);
            baseResponse.setMsg("恢复定时任务失败");
            logger.error("Execute API /scheduleJob/resumeScheduleJob failed!", e);
        } catch (Exception e) {
            baseResponse.exception();
            logger.error("Execute API /scheduleJob/resumeScheduleJob failed!", e);
        }
        return baseResponse;
    }

    /**
     * 删除任务
     *
     * @param jobKeyRequest
     * @return
     */
    @PostMapping(value = "/deleteScheduleJob")
    public BaseResponse deleteScheduleJob(@RequestBody JobKeyRequest jobKeyRequest) {
        BaseResponse baseResponse = new BaseResponse();
        try {
            scheduleJobService.deleteScheduleJob(jobKeyRequest);
            baseResponse.success();
        } catch (SchedulerException e) {
            baseResponse.setCode(BaseMsg.CODE_EXCEPTION);
            baseResponse.setMsg("删除定时任务失败");
            logger.error("Execute API /scheduleJob/deleteScheduleJob failed!", e);
        } catch (Exception e) {
            baseResponse.exception();
            logger.error("Execute API /scheduleJob/deleteScheduleJob failed!", e);
        }
        return baseResponse;
    }

    /**
     * 立即执行job
     *
     * @param jobKeyRequest
     * @return
     */
    @PostMapping(value = "/executeJobNow")
    public BaseResponse executeJobNow(@RequestBody JobKeyRequest jobKeyRequest) {
        BaseResponse baseResponse = new BaseResponse();
        try {
            scheduleJobService.executeJobNow(jobKeyRequest);
            baseResponse.success();
        } catch (SchedulerException e) {
            baseResponse.setCode(BaseMsg.CODE_EXCEPTION);
            baseResponse.setMsg("执行定时任务失败");
            logger.error("Execute API /scheduleJob/executeJobNow failed!", e);
        } catch (Exception e) {
            baseResponse.exception();
            logger.error("Execute API /scheduleJob/executeJobNow failed!", e);
        }
        return baseResponse;
    }

    /**
     * 获取所有计划中的任务列表
     *
     * @return
     */
    @PostMapping(value = "/getAllJobs")
    public BaseResponse getAllJobs() {
        BaseResponse baseResponse = new BaseResponse();
        try {
            List<ScheduleJob> allJobList = scheduleJobService.getAllJobs();
            baseResponse.success(allJobList);
        } catch (SchedulerException e) {
            baseResponse.setCode(BaseMsg.CODE_EXCEPTION);
            baseResponse.setMsg("执行定时任务失败");
            logger.error("Execute API /scheduleJob/getAllJobs failed!", e);
        } catch (Exception e) {
            baseResponse.exception();
            logger.error("Execute API /scheduleJob/getAllJobs failed!", e);
        }
        return baseResponse;
    }

    /**
     * 所有正在运行的job
     *
     * @return
     */
    @PostMapping(value = "/getRunningJobs")
    public BaseResponse getRunningJobs() {
        BaseResponse baseResponse = new BaseResponse();
        try {
            List<ScheduleJob> runningJobList = scheduleJobService.getRunningJobs();
            baseResponse.success(runningJobList);
        } catch (SchedulerException e) {
            baseResponse.setCode(BaseMsg.CODE_EXCEPTION);
            baseResponse.setMsg("执行定时任务失败");
            logger.error("Execute API /scheduleJob/getRunningJobs failed!", e);
        } catch (Exception e) {
            baseResponse.exception();
            logger.error("Execute API /scheduleJob/getRunningJobs failed!", e);
        }
        return baseResponse;
    }

    /**
     * 检查执行任务的类和方法是否存在
     *
     * @return
     */
    @PostMapping(value = "/checkClassAndMethod")
    public BaseResponse checkClassAndMethod(@RequestBody ScheduleJob scheduleJob) {
        BaseResponse baseResponse = new BaseResponse();
        try {
            List<ScheduleJob> runningJobList = scheduleJobService.getRunningJobs();
            baseResponse.success(runningJobList);
        } catch (SchedulerException e) {
            baseResponse.setCode(BaseMsg.CODE_EXCEPTION);
            baseResponse.setMsg("执行定时任务失败");
            logger.error("Execute API /scheduleJob/getRunningJobs failed!", e);
        } catch (Exception e) {
            baseResponse.exception();
            logger.error("Execute API /scheduleJob/getRunningJobs failed!", e);
        }
        return baseResponse;
    }


    @GetMapping(value = "/querySchedulejob")
    public Map<String, Object> querySchedulejob(@RequestParam(value = "pageNum") Integer pageNum, @RequestParam(value = "pageSize") Integer pageSize) {
        //PageInfo<JobAndTrigger> jobAndTrigger = iJobAndTriggerService.getJobAndTriggerDetails(pageNum, pageSize);
        Map<String, Object> map = new HashMap<String, Object>();
        //map.put("JobAndTrigger", jobAndTrigger);
        //map.put("number", jobAndTrigger.getTotal());
        return map;
    }


}
